package com.example.yucheng.controller;

import com.example.yucheng.domain.Loss;
import com.example.yucheng.domain.OutboundGood;
import com.example.yucheng.domain.OutboundUnit;
import com.example.yucheng.domain.Stock;
import com.example.yucheng.service.OutboundService;
import com.example.yucheng.service.StockService;
import com.example.yucheng.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/outbound")
@CrossOrigin
public class OutboundController {
    @Autowired
    OutboundService outboundService;
    @Autowired
    StockService stockService;


    @GetMapping("/getOutboundGoodList")
    public Result getOutboundGoodList(@RequestParam Integer pageNum, @RequestParam Integer pageSize ,@RequestParam(required = false) Integer gId,@RequestParam(required = false) Integer rId) throws ParseException {
        Map<String, Object> map = new HashMap<>();
        pageNum -=1;
        pageNum *= pageSize;
       List<OutboundGood> outboundGoodList = outboundService.getOutboundGoodList(gId);
//       for(OutboundGood outboundGood : outboundGoodList){
//           if (outboundGood == null){
//               outboundGoodList.remove(outboundGood);
//           }
//       }
        List<OutboundGood> outboundGoodList2=new ArrayList<>();
       if (rId != null){
           for(OutboundGood outboundGood : outboundGoodList){
              List<OutboundUnit> outboundUnitList =  outboundService.getboundUnit(outboundGood.getId());
              for(OutboundUnit outboundUnit : outboundUnitList){
                  if (outboundUnit.getRId() == rId){
                      outboundGoodList2.add(outboundGood);
                      break;
                  }
              }
           }
       }else{
           for(OutboundGood outboundGood : outboundGoodList) {
               List<OutboundUnit> outboundUnitList = outboundService.getboundUnit(outboundGood.getId());
               if (outboundUnitList == null){
                   outboundGoodList.remove(outboundGood);
               }
           }
           outboundGoodList2 = outboundGoodList;
       }
        List<OutboundGood> outboundGoodList3=new ArrayList<>();
       for(int i = pageNum;i<=pageNum+pageSize && i<outboundGoodList2.size();i++){
           outboundGoodList3.add(outboundGoodList2.get(i));
       }
       map.put("total",outboundGoodList2.size());
       map.put("outboundGoodList",outboundGoodList3);
       return Result.success(map);
    }

    @GetMapping("/getOutboundGoodDetailList")
    public Result getOutboundOrderDetail(@RequestParam Integer oId){
        Map<String, Object> map = new HashMap<>();
        List<OutboundUnit> outboundUnitList = outboundService.getOutboundOrderDetail(oId);
        double sumMoney = 0;
        for(int i =0;i<outboundUnitList.size();i++){
           sumMoney += outboundUnitList.get(i).getCostPrice() * outboundUnitList.get(i).getNumber();
        }
        List<OutboundGood> outboundGoodList = outboundService.getGoodInfo(oId);
        map.put("outboundReceivingUnitList" , outboundUnitList);
        map.put("goodName",outboundGoodList.get(0).getGoodName());
        map.put("categoryName",outboundGoodList.get(0).getCategoryName());
        map.put("remark",outboundGoodList.get(0).getRemark());
        map.put("sumMoney",sumMoney);
        return Result.success(map);
    }

    @PutMapping("/updateOutboundDetail")
    public Result updateOutboundDetail(@RequestBody OutboundUnit outboundUnit , @RequestParam(required = false) Integer gId){
        if (gId != null) {
            OutboundGood outboundGood = outboundService.getOutboundByGid(gId);
            if (outboundGood == null){
                outboundService.addGoodList(gId," ");
            }
            OutboundGood outboundGood2 = outboundService.getOutboundByGid(gId);
            outboundUnit.setOId(outboundGood2.getId());
            outboundService.updateOutboundDetail(outboundUnit);
//            outboundService.updateOutboundGoodInfo(outboundGood.getId(), gId);
        }else{
            outboundService.updateOutboundDetail(outboundUnit);
        }
        return Result.success("修改成功");
    }

    @DeleteMapping("/deleteOutbound")
    public Result deleteOutbound(@RequestParam Integer [] ids){
        for(int id : ids)
            outboundService.deleteOutboundUnit(id);
        return Result.success("删除成功");
    }

    @PostMapping("/addOutbound")
    public Result addOutboundOrder(@RequestParam Integer rId,@RequestParam String receivingPerson,@RequestParam String department,@RequestParam String remark,@RequestBody OutboundGood outboundGood){
        System.out.println(outboundGood.getStockList().get(0).getId());
        List<Stock> goodList = outboundGood.getStockList();
        //添加不存在的商品列表到出库列表
        for(Stock good : goodList){
            OutboundGood outboundGood1 = outboundService.getOutboundByGid(good.getId());
            if (outboundGood1 == null)
                outboundService.addGoodList(good.getId(),remark);
        }
        //添加详细信息
        for(Stock good : goodList){
            //找oid
            int oId = outboundService.getOutGoodIdByGid(good.getId());

            outboundService.addOutboundUnit(rId,receivingPerson,department,good,oId,good.getNumber(),good.getRemark(),good.getPer(),good.getCostPrice());
            //更新库存数量
            Stock orginStock = stockService.findGoodNumber(good.getId());
//            Stock stock = orginStock;
//            stock.setId(good.getId());
            orginStock.setNumber(orginStock.getNumber() - good.getNumber());
            stockService.updateStock(orginStock);
            //
        }

        return Result.success("商品出库成功");
    }

    @GetMapping("getLossList")
    public Result getLossList(){
        Map<String,Object> map = new HashMap<>();
        List<Loss> lossList = outboundService.getLossList();
        map.put("lossList",lossList);
        return Result.success(map);
    }

    @PutMapping("addLoss")
    public Result addLoss(@RequestBody Loss loss){
        outboundService.addLoss(loss);
        Stock stock = stockService.findGoodNumber(loss.getGId());
//        if (stock.getNumber() >= loss.getNumber())
        stock.setNumber(stock.getNumber() - loss.getNumber());
//        else return Result.success("商品库存数量不足");
        stockService.updateStock(stock);
        return Result.success("添加成功");
    }

    @DeleteMapping("deleteLoss")
    public Result deleteLoss(@RequestParam int [] ids){
        for(int id : ids){
            Loss loss = outboundService.getLossById(id);
            Stock stock = stockService.findGoodNumber(loss.getGId());
            stock.setNumber(stock.getNumber() + loss.getNumber());
            stockService.updateStock(stock);
            outboundService.deleteLossById(id);
        }
        return Result.success("删除成功");
    }


    @GetMapping("findGoodName")
    public Result findGoodName(@RequestParam String text){
        Map<String,Object> map = new HashMap<>();
        String goodName = outboundService.findGoodName(text);
        map.put("goodName",goodName);
        return Result.success(map);
    }


}
